<?php
    Yii::app()->clientScript->registerScriptFile(Yii::app()->request->baseUrl . '/js/sprintf.js');
    Yii::app()->clientScript->registerScriptFile(Yii::app()->request->baseUrl . '/js/calculatepayment.js');

    if (isset($id4clear) || isset($_GET['id'])) {
        //load need cleared loan setting record
        if (isset($id4clear)) {
            $id = $id4clear;
        } elseif (isset($_GET['id'])) {
            $id = $_GET['id'];
        } else {
            unset($id);
        }
        if (isset($id)) {
            $loansetting = CustomerLoanSetting::model()->findByPk($id);
            $customer = $loansetting->customer;
        }
    }

    $form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
        'id' => 'customer-loan-setting-form',
        'enableAjaxValidation' => true,
    ));
?>

<p class="help-block">Fields with <span class="required">*</span> are required.</p>

<div class="form-actions">
    <?php
        $this->widget('bootstrap.widgets.TbButton', array(
            'buttonType' => 'submit',
            'type' => 'primary',
            'label' => t($model->isNewRecord ? 'Create' : 'Save'),
            //'htmlOptions' => array('class' => 'span2'),
            'icon' => 'icon-ok icon-white',
        ));
	?>
    <?php
        $this->widget('bootstrap.widgets.TbButton', array(
            'buttonType' => 'Button',
            'type' => 'primary',
            'label' => t('Cancel'),
            'url' => Yii::app()->createUrl('customerloansetting/index'),
            //'htmlOptions' => array('class' => 'span2'),
            'icon' => 'icon-remove icon-white',
        ));
    ?>
</div>

<?php echo $form->errorSummary($model); ?>

<?php
    echo $form->textFieldRow($model, 'customer_id', array(
        'class' => 'span3',
        'readonly' => true,
        'value' => isset($id) ? $loansetting->customer_id : '',
    ));
?>

<!-- model: select customer, start -->
<?php
    //if id4clear is set, the Modal for select customer is not needed, the select button is not shown
    if (!isset($id4clear) && !isset($_GET['id'])) {
        $this->beginWidget('bootstrap.widgets.TbModal', array('id' => 'chooseCustomerModal'));
        ?>

        <div class="modal-header">
            <a class="close" data-dismiss="modal">&times;</a>
            <h4><?php echo t('Choose Customer'); ?></h4>
        </div>

        <div class="modal-body">
            <?php
            //list customers who under the user
            $customers = new Customers;
            $criteria = new CDbCriteria;
            $criteria->select = 'id, customer_code, firstname, lastname, newic, last_loan_num';
            $user = User::model()->findByPk(Yii::app()->user->id);
            if ($user->user_code <> '')
                $criteria->condition = 'left(customer_code, 3)="' . $user->user_code . '"';
            $criteria->order = 'customer_code asc';

            $dataProvider = new CActiveDataProvider($customers, array(
                'criteria' => $criteria,
                'pagination' => array(
                    'pageSize' => 10,
                ),
            ));

            $this->widget('bootstrap.widgets.TbGridView', array(
                'type' => 'bordered',
                'dataProvider' => $dataProvider,
                'filter' => $customers,
                'template' => "{items}{pager}",
                'columns' => array(
                    array('name' => 'id', 'header' => '#ID'),
                    array('name' => 'customer_code', 'header' => t('Customer Code')),
                    array('name' => 'firstname', 'header' => t('First Name')),
                    array('name' => 'lastname', 'header' => t('Last Name')),
                    array('name' => 'newic', 'header' => t('New IC')),
                    array(
                        'class' => 'bootstrap.widgets.TbButtonColumn',
                        'htmlOptions' => array(
                            'style' => 'width: 50px',
                        ),
                        'template' => '{chooseCustomer}',
                        'buttons' => array(
                            'chooseCustomer' => array(
                                'label' => 'Choose', // text label of the button
                                'url' => '$data["id"]."|".$data["customer_code"]."|".$data["last_loan_num"]', // a PHP expression for generating the URL of the button
                                'imageUrl' => Yii::app()->request->baseUrl . '/images/choose-32.png', // image URL of the button. If not set or false, a text link is used
                                'options' => array(
                                    'onclick' => 'var param=$(this).attr(\'href\');
                                var params=param.split(\'|\');
                                document.getElementById(\'CustomerLoanSetting_customer_id\').value = params[0];
                                document.getElementById(\'CustomerLoanSetting_customer_code\').value = params[1];
                                document.getElementById(\'CustomerLoanSetting_loan_code\').value = sprintf("%s%04d", params[1], parseInt(params[2])+1);',
                                    'data-dismiss' => 'modal',
                                //'icon' => 'icon-ok',
                                ), // HTML options for the button tag
                                'visible' => 'true', // a PHP expression for determining whether the button is visible
                            )
                        ),
                    ),
                ),
            ));
            ?>
        </div>

        <div class="modal-footer">
            <?php
            $this->widget('bootstrap.widgets.TbButton', array(
                'label' => t('Close'),
                'url' => '#',
                'htmlOptions' => array('data-dismiss' => 'modal'),
            ));
            ?>
        </div>

        <?php $this->endWidget(); ?>
        <!-- the following style is used for hack TbButton's position, that move up 10px to align with input box. -->
        <style>
            .aligntop {margin-bottom: 10px;}
        </style>
        <!-- hack end -->

        <?php
        $this->widget('bootstrap.widgets.TbButton', array(
            'label' => t('Choose...'),
            'type' => 'primary',
            'htmlOptions' => array(
                'data-toggle' => 'modal',
                'data-target' => '#chooseCustomerModal',
                'class' => 'aligntop',
            ),
        ));
    } //end of !isset($id4clear)
?>
<!-- model: select customer, end -->
<?php
    echo $form->textFieldRow($model, 'customer_code', array('class' => 'span3',
        'maxlength' => 20,
        'readonly' => true,
        'value' => isset($id) ? $loansetting->customer_code : '',
    ));
?>

<?php
    echo $form->textFieldRow($model, 'loan_code', array('class' => 'span3',
        'maxlength' => 20,
        'readonly' => true,
        'value' => isset($id) ? sprintf('%s%04d', $loansetting->customer_code, $customer->last_loan_num + 1) : '',
    ));
?>

<?php
    echo $form->textFieldRow($model, 'loan_amount', array('class' => 'span2',
        'maxlength' => 15,
        'onkeyup' => 'this.value=this.value.replace(/[^\d.]/,\'\')',
        'onblur' => 'this.value=sprintf(\'%.2f\', this.value);',
        'onfocus' => 'this.select();',
    ));
?>

<?php echo $form->textFieldRow($model, 'interest', array('class' => 'span1', 'readonly' => 'readonly', 'append' => '%')); ?>

<?php
    echo $form->dropDownListRow($model, 'period_type', array('Day' => t('Day'), 'Month' => t('Month'), 'Week' => t('Week')), array('class' => 'span1',
        'maxlength' => 20,
        'onchange' => 'var period_type=document.getElementById(\'CustomerLoanSetting_period_type\');
        var days_of_period= document.getElementById(\'CustomerLoanSetting_days_of_period\');
        if (period_type.value==\'Day\') {
            days_of_period.value=1;
        } else if (period_type.value==\'Week\') {
            days_of_period.value=7;
        } else if (period_type.value==\'Month\') {
            days_of_period.value=30;
        };'));
?>

<?php
    echo $form->textFieldRow($model, 'days_of_period', array('class' => 'span1',
        'readonly' => true,
    ));
?>

<?php
    echo $form->textFieldRow($model, 'period', array('class' => 'span1',
        'onkeyup' => "this.value=this.value.replace(/[^\d]/,'')",
        'onblur' => 'document.getElementById(\'CustomerLoanSetting_installments\').value=this.value',
        'onfocus' => 'this.select();',
    ));
?>

<?php
    echo $form->textFieldRow($model, 'installments', array('class' => 'span1',
        'readonly' => true,
        'onfocus' => 'this.select();',
    ));
?>

<?php
    echo $form->textFieldRow($model, 'paying_amount', array('class' => 'span2',
        'maxlength' => 15,
        'onkeyup' => 'this.value=this.value.replace(/[^\d.]/,\'\')',
        'onblur' => 'this.value=sprintf(\'%.2f\', this.value); calculatepayment();',
        'onfocus' => 'this.select();',
    ));
?>

<?php
    echo $form->textFieldRow($model, 'payment_per_installment', array('class' => 'span2',
        'maxlength' => 15,
        'onkeyup' => 'this.value=this.value.replace(/[^\d.]/,\'\')',
        'onfocus' => 'this.select();',
    ));
?>

<?php
    echo $form->textFieldRow($model, 'last_period_payment', array('class' => 'span2',
        'maxlength' => 15,
        'onkeyup' => 'this.value=this.value.replace(/[^\d.]/,\'\')',
        'onfocus' => 'this.select();',
    ));
?>

<?php
    echo $form->textFieldRow($model, 'withdraw_amount', array('class' => 'span2',
        'maxlength' => 15,
        'onkeyup' => 'this.value=this.value.replace(/[^\d.]/,\'\')',
        'onblur' => "this.value=sprintf('%.2f', this.value - " . (isset($owedloan) ? $owedloan : 0) . ")",
        'onfocus' => 'this.select();',
    ));
?>

<?php
    echo $form->checkBoxRow($model, 'active', array('class' => 'span1',
        'maxlength' => 1,
        'checked' => true,
        'readonly' => true,
        'onclick' => 'if (this.readonly) {return false;};',
    ));
?>

<?php
    echo $form->dropDownListRow($model, 'status', array('Paying' => t('Paying'),
        'Clear' => t('Clear'),
            ), array('class' => 'span2',
        'maxlength' => 20,
        'readonly' => true,
        'value' => t('Paying'),
    ));
?>

<?php //echo $form->textFieldRow($model,'createtime',array('class'=>'span3'));        ?>

<?php echo $form->hiddenRow($model, 'updatedby_user_id', array('value' => Yii::app()->user->id)); ?>

<?php //echo $form->textFieldRow($model,'modifytime',array('class'=>'span3'));         ?>

<div class="form-actions">
    <?php
        $this->widget('bootstrap.widgets.TbButton', array(
            'buttonType' => 'submit',
            'type' => 'primary',
            'label' => t($model->isNewRecord ? 'Create' : 'Save'),
            //'htmlOptions' => array('class' => 'span2'),
            'icon' => 'icon-ok icon-white',
        ));
	?>
    <?php
        $this->widget('bootstrap.widgets.TbButton', array(
            'buttonType' => 'Button',
            'type' => 'primary',
            'label' => t('Cancel'),
            'url' => Yii::app()->createUrl('customerloansetting/index'),
            //'htmlOptions' => array('class' => 'span2'),
            'icon' => 'icon-remove icon-white',
        ));
    ?>
</div>

<?php $this->endWidget(); ?>
